﻿using CKS.Core.Framework;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ZR.Model.Enum;
using ZR.Model.Models.ProductionControl;

namespace CKS.PLC.Action.Subscription.OP50
{
    /// <summary>
    /// 翅片中板盖板 结果上传
    /// </summary>
    public class DB3011_Work01Action : PLCAction
    {
        public DB3011_Work01Action(DecodeValue _value) : base(_value)
        {
        }
        public override void Execute()
        {
            var replayDB = 3010;
            var replayAction = 1;
            var replyPos = Value.GetPosition(replayDB, replayAction);
            var result = Value.Value.ToInt();
          
            var saveconfirm = ReadInt16(replyPos);

            if (result == 1)
            {
                if (saveconfirm == 1)
                    return;
                //还使用到了翅片，查询翅片类型的子篮，零件数量，进行扣减，一次查两框，若第一框扣减完成、则扣减第2框
                string batchNo = GetPartBatchNo((int)PartTypeEnum.翅片);
                UpdateBasketStock(PartTypeEnum.翅片);

                //保存op50数据结果-
                DcOp50Record dcOp50 = new DcOp50Record() { 
                    CoverCode = Value.GetLinkData(1).ToString(),
                    MiddlePlateBasketCode = Value.GetLinkData(2).ToString(),
                    WeldedPlateBasketCode = Value.GetLinkData(3).ToString(),
                    RoundHoleInspection1 = Value.GetLinkData(4).ToDecimal(),
                    RoundHoleInspection2 = Value.GetLinkData(5).ToDecimal(),
                    CylinderInspection1 = Value.GetLinkData(6).ToDecimal(),
                    CylinderInspection2 = Value.GetLinkData(7).ToDecimal(),
                    WarpageValue = Value.GetLinkData(8).ToDecimal(),
                    Flatness = Value.GetLinkData(9).ToDecimal(),
                    ProfileXDistance = Value.GetLinkData(10).ToDecimal(),
                    ProfileYDistance = Value.GetLinkData(11).ToDecimal(),
                    PositionDegreeTopLeftCircle = Value.GetLinkData(12).ToDecimal(),
                    PositionDegreeBottomRightCircle = Value.GetLinkData(13).ToDecimal(),
                    Thickness = Value.GetLinkData(14).ToDecimal(),
                    LeftFlatness = Value.GetLinkData(15).ToDecimal(),
                    LeftQuantity = Value.GetLinkData(16).ToInt(),
                    MaxSpacingLeftHead = Value.GetLinkData(17).ToDecimal(),
                    MaxSpacingLeftMiddle = Value.GetLinkData(18).ToDecimal(),
                    MaxSpacingLeftTail = Value.GetLinkData(19).ToDecimal(),
                    RightFlatness = Value.GetLinkData(20).ToDecimal(),
                    RightQuantity = Value.GetLinkData(21).ToInt(),
                    MaxSpacingRightHead = Value.GetLinkData(22).ToDecimal(),
                    MaxSpacingRightMiddle = Value.GetLinkData(23).ToDecimal(),
                    MaxSpacingRightTail = Value.GetLinkData(24).ToDecimal(),

                    TotalResult = Value.GetLinkData(25).ToInt(),
                    MidPltInspectRes = Value.GetLinkData(26).ToInt(),
                    WeldTabInspectRes = Value.GetLinkData(27).ToInt(),
                    FinInspectRes = Value.GetLinkData(28).ToInt(),
                    LocationId = Value.GetLinkData(29).ToInt(),
                    UpdateTime = DateTime.Now,
                    CreatedTime = DateTime.Now,
                    
                };
                //如果存在则更新，否则新增
               // db.Queryable<DcOp50Record>().Where(a => a.CoverCode == dcOp50.CoverCode).First();

                dcOp50.UpdateTime = dcOp50.CreatedTime = DateTime.Now;
                db.Insertable(dcOp50).ExecuteCommand();

                
                
                var replayPos = Value.GetPosition(replayDB, replayAction);
                AckWriteInt16(replayPos, 1);
                EventBus.AddLog($"OP50请求保存数据 {dcOp50.CoverCode}");
            }
            else
            {
                var replayPos = Value.GetPosition(replayDB, replayAction);
                AckWriteInt16(replayPos, 0);
                EventBus.AddLog("OP50请求保存数据-状态重置");
            }
        }
    }
}
